package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;

@RestController
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "用户订单功能")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * @param ordersSubmitDTO: return Result<OrderSubmitVO>
     *                         {@link Result<OrderSubmitVO>}
     * @author fenglai
     * @date 2023/12/4 21:08
     * @description 用户下单功能
     */
    @ApiOperation("用户下单功能")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> userSubmit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) throws URISyntaxException, IOException {

        log.info("正在执行用户下单功能..........下单信息为:{}", ordersSubmitDTO);

        OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO);

        return Result.success(orderSubmitVO);

    }

    /**
     * @param id: return Result<Orders>
     *            {@link Result<Orders>}
     * @author fenglai
     * @date 2023/12/4 21:08
     * @description 根据订单id查询订单功能
     */

    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> selectOrder(@PathVariable Long id) {

        log.info("正在执行根据订单id查询订单的功能.........id为:{}", id);

        OrderVO orders = orderService.selectOrder(id);

        return Result.success(orders);
    }


    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */

    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success();
    }

    /**
     * @param ordersPageQueryDTO: return Result<PageResult>
     *                            {@link Result<PageResult>}
     * @author fenglai
     * @date 2023/12/4 21:20
     * @description 历史订单查询功能
     */
    @ApiOperation("历史订单查询功能")
    @GetMapping("/historyOrders")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO) {

        log.info("正在执行历史订单查询的功能............查询的信息为:{}", ordersPageQueryDTO);

        PageResult ordersVO = orderService.historyOrders(ordersPageQueryDTO);

        return Result.success(ordersVO);
    }


    /**
     * @param id: return Result
     *            {@link Result}
     * @author fenglai
     * @date 2023/12/4 21:37
     * @description 再来一单的功能
     */
    @ApiOperation("再来一单的功能")
    @PostMapping("/repetition/{id}")
    public Result repetitionOrder(@PathVariable Long id) {

        log.info("正在执行在再来一单的功能.........订单id为:{}", id);

        orderService.repetitionOrder(id);

        return Result.success();
    }

    /**
     * @param id: return Result
     * @author fenglai
     * @date 2023/12/5 20:18
     * @description 用户取消订单功能
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("用户取消订单")
    public Result cancelOrder(@PathVariable Long id) {

        log.info("正在执行用户取消的功能..................用户id为:{}", id);

        orderService.cancelOrder(id);

        return Result.success();

    }

    @ApiOperation("用户催单功能")
    @GetMapping("/reminder/{id}")
    public Result reminderOrder(@PathVariable Long id) {

        log.info("正在执行用户催单的功能................用户id为:{}", id);

        orderService.reminderOrder(id);

        return Result.success();
    }


}
